﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using mmoss.orm;
using mmoss.classes;
using mmoss.classes.Interfaces;
using mmoss.application.classes;

namespace mmoss.application
{
    public partial class CardProcess : Form
    {
        public Cart CurrentCart { get; set; }
        public Patient CurrentPatient { get; set; }
        public string transactionID { get; set; }
        public IEComm CurrentEComm { get; set; }
        public CardProcess()
        {
            InitializeComponent();
            this.Load += new EventHandler(CardProcess_Load);
        }

        void CardProcess_Load(object sender, EventArgs e)
        {
            txtCardNum.KeyUp += new KeyEventHandler(txtCardNum_KeyUp);
            timer1.Tick += new EventHandler(timer1_Tick);
            txtAmount.Text = string.Format("{0:c}", CurrentCart.cartTotal);
        }

        void txtCardNum_KeyUp(object sender, KeyEventArgs e)
        {
            if (((TextBox)sender).Text == "") return;
            string sLastChar = ((TextBox)sender).Text.Substring(((TextBox)sender).Text.Length - 1, 1);
            if (timer1.Enabled)
            {
                //keep timer going
                timer1.Stop();
                timer1.Start();
            }
            if (sLastChar == "%")
            {
                //begin TIMER
                timer1.Enabled = true;
                timer1.Start();
            }
            else if (sLastChar == "?")
            {
                //end character -- wait for tic to disable to stop timer and parse
            }
        }

        void timer1_Tick(object sender, EventArgs e)
        {
            //timer ticked
            timer1.Stop();
            timer1.Enabled = false;
            processCard();
        }
        private void processCard()
        {
            try
            {
                string sScan = txtCardNum.Text;
                txtCardNum.Text = sScan.Split('^')[0].Replace("%B", "");
                txtNameCard.Text = sScan.Split('^')[1].Split('/')[1].Trim() + " " + sScan.Split('^')[1].Split('/')[0].Trim();
                txtExpDate.Text = sScan.Split('^')[2].Split('=')[1].Substring(2, 2) + "/" + sScan.Split('^')[2].Split('=')[1].Substring(0, 2);
                btnProcess.Enabled = false;
                picProcessing.Visible = true;

                //process the card
                //todo - hard-code the implementation right now
                IEComm l_ecomm = new AuthNet();
                l_ecomm.CCAmount = CurrentCart.cartTotal;
                l_ecomm.APIkey = ConfigurationManager.AppSettings.Get("eCommAuthorizeNetTRANSACTIONKEY");
                //put invoice # in here
                l_ecomm.CCDescription = "Test";
                l_ecomm.CCExpDate = txtExpDate.Text.Replace("/", string.Empty);
                l_ecomm.CCNumber = txtCardNum.Text;
                l_ecomm.Delimited = true;
                l_ecomm.DelimitedChar = '|';
                l_ecomm.Login = ConfigurationManager.AppSettings.Get("eCommAuthorizeNetLOGINID");
                l_ecomm.Password = "test";
                l_ecomm.Patient = CurrentPatient;
                l_ecomm.PostURL = "https://secure.authorize.net/gateway/transact.dll";
                l_ecomm.TransType = "AUTH_CAPTURE";

                //todo - in test mode
                l_ecomm.TestMode = true;
                l_ecomm.Charge();
                //todo put in logic for bad charge here
                CurrentEComm = l_ecomm;
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error in processing.  Please check card number and try again");
            }

        }
        private void btnProcess_Click(object sender, EventArgs e)
        {
            processCard();
        }

    }
}
